package com.jd.org.service.impl;


import com.jd.basic.query.DepartmentQuery;
import com.jd.basic.util.PageList;
import com.jd.org.domain.Department;
import com.jd.org.mapper.DepartmentMapper;
import com.jd.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    @Transactional
    public void save(Department department) {
        departmentMapper.save(department);
    }

    @Override
    @Transactional
    public void remove(Long id) {
        departmentMapper.remove(id);
    }

    @Override
    @Transactional
    public void update(Department department) {
        departmentMapper.update(department);
    }

    @Override
    public Department loadById(Long id) {
        return departmentMapper.loadById(id);
    }

    @Override
    public List<Department> loadAll() {
        return departmentMapper.loadAll();
    }

    @Override
    public PageList<Department> queryPage(DepartmentQuery departmentQuery) {
        //1.查询总条数
        Integer totals = departmentMapper.queryCount(departmentQuery);
        //如果总数为0，没有必要去查询分页数据了 - 反正查询出来都是一个空集合
        if(totals==0){
            return new PageList<>();
        }

        //2.查询当前页的分页数据
        List<Department> data = departmentMapper.queryPage(departmentQuery);
        return new PageList<>(totals,data);
    }
}
